package tx.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import tx.dao.PhoneDao;

/**
 * @author ddy
 * @create 2020-08-17 09:42:50
 */
@Repository
public class PhoneDaoImpl implements PhoneDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据手机的id查询价格
     *
     * @param id
     * @return
     */
    public int findPriceById(String id) {
        String sql = "SELECT price FROM phone WHERE id=?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, id);
        return price;
    }

    /**
     * 更新用户账户余额
     *
     * @param userName
     * @param price
     */
    public void updateBalance(String userName, int price) {
        String sql = "UPDATE account SET balance=balance-? WHERE username=?";
        jdbcTemplate.update(sql, price, userName);
    }

    /**
     * 更新手机库存
     *
     * @param id
     */
    public void updateStock(String id) {
        String sql = "UPDATE phone_stock SET stock=stock-1 WHERE id=?";
        jdbcTemplate.update(sql, id);
    }
}
